	package elements.axis ;
	import flash.text.TextField;
	import flash.display.Sprite;
	import flash.text.TextFormat;
	import ColorConverter.Color;
	import string.Utils;
	
	class RadarAxisLabels extends Sprite{

		var style : {
			color		: Color
			, steps		: Int
			, labels	: Array<String>
		};
		public var labels:Array<String>;
		
		
		public function new( json:Dynamic ) {
			super();

			// default values
			this.style = {
				color		: Hex('#784016')
				, steps		: 1
				, labels	: new Array<String>() // TODO - compare with json object, if json object is filled unlike this one, replace with json object label values
			};
			
			if( json != null )
				ObjectHelper.merge_2( json, this.style );
				
			//this.style.color = Utils.get_color( this.style.color );
			
			// cache the text for tooltips
			this.labels = new Array<String>();
			var values:Array<Dynamic>;
			var ok:Bool = false;
			
			if( this.style.labels.length > 0 )
			{
				
				for ( s in this.style.labels )
					this.add( s, this.style );
			}
			
		}

		public function add( label:Dynamic, style:Dynamic ) : Void
		{
			var label_style : 
			{
				color 		: Color
				, text		: String
				, size		: Int
				, visible	: Bool
			} // TODO - this struct popped up somewhere else, recycle definitions!
			=
			{
				color:		style.color,
				text:		'',
				size:		style.size,
				visible:	true
			};

			if(Std.is(label,String ))
				label_style.text = cast(label,String);
			else {
				ObjectHelper.merge_2( label, label_style );
			}

			// our parent color is a number, but
			// we may have our own color:
			//if(Std.is(label_style.color,String ))
			//	label_style.color = Utils.get_color( label_style.color );

			this.labels.push( label_style.text );

			//
			// inheriting the 'visible' attribute
			// is complex due to the 'steps' value
			// only some labels will be visible
			//
			if( label_style.visible == null )
			{
				//
				// some labels will be invisible due to our parents step value
				//
				if ( ( (this.labels.length - 1) % style.steps ) == 0 )
					label_style.visible = true;
				else
					label_style.visible = false;
			}

			var l:TextField = this.make_label( label_style );
			this.addChild( l );
		}
		
		public function make_label( label_style:Dynamic ):TextField {
			
			// we create the text in its own movie clip
			
			var tf:TextField = new TextField();
            		tf.x = 0;
			tf.y = 0;
			tf.text = label_style.text;
			
			var fmt:TextFormat = new TextFormat();
			fmt.color = ColorConverter.toFloat(label_style.color);
			fmt.font = "Verdana";
			fmt.size = label_style.size;
			fmt.align = flash.text.TextFormatAlign.RIGHT;
			tf.setTextFormat(fmt);
			
			tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
			tf.visible = label_style.visible;
			return tf;
		}
		
		// move y axis labels to the correct x pos
		public function resize( sc:ScreenCoordsRadar ):Void {

			var tf:TextField;
			var center: Float = sc.get_center_x();
			
			for ( i in 0...this.numChildren ) {
				// right align
				tf = cast(this.getChildAt(i),TextField);
				tf.x = center - tf.width;
			}
			
			// now move it to the correct Y, vertical center align
			for ( i in 0...this.numChildren ) {
				
				tf = cast(this.getChildAt(i),TextField);
				tf.y = ( sc.get_y_from_val( i, false ) - (tf.height / 2) );
			}
		}
		
		public function die(): Void {
			
			this.style = null;
			this.labels = null;
			
			this.graphics.clear();
			while ( this.numChildren > 0 )
				this.removeChildAt(0);
		}
	}
